package com.zhaoxi.project.wms.service.impl;

import java.util.List;

import com.zhaoxi.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;

import com.zhaoxi.common.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.zhaoxi.project.wms.domain.WmsOrderLine;
import com.zhaoxi.project.wms.mapper.WmsOrderMapper;
import com.zhaoxi.project.wms.domain.WmsOrder;

/**
 * 跟物品有关的订单Service业务层处理
 *
 * @author zhaoxi
 * @date 2023-07-22
 */
@Service
public class WmsOrderServiceImpl {
    @Autowired
    private WmsOrderMapper wmsOrderMapper;

    /**
     * 查询跟物品有关的订单
     *
     * @param id 跟物品有关的订单主键
     * @return 跟物品有关的订单
     */

    public WmsOrder selectWmsOrderById(Long id) {
        return wmsOrderMapper.selectWmsOrderById(id);
    }

    /**
     * 查询跟物品有关的订单列表
     *
     * @param wmsOrder 跟物品有关的订单
     * @return 跟物品有关的订单
     */

    public List<WmsOrder> selectWmsOrderList(WmsOrder wmsOrder) {
        return wmsOrderMapper.selectWmsOrderList(wmsOrder);
    }

    /**
     * 新增跟物品有关的订单
     *
     * @param wmsOrder 跟物品有关的订单
     * @return 结果
     */
    @Transactional
    public int insertWmsOrder(WmsOrder wmsOrder) {
        wmsOrder.setCreateTime(DateUtils.getNowDate());
        int rows = wmsOrderMapper.insertWmsOrder(wmsOrder);
        this.insertWmsOrderLine(wmsOrder);
        return rows;
    }

    /**
     * 修改跟物品有关的订单
     *
     * @param wmsOrder 跟物品有关的订单
     * @return 结果
     */
    @Transactional
    public int updateWmsOrder(WmsOrder wmsOrder) {
        wmsOrder.setUpdateTime(DateUtils.getNowDate());
        wmsOrderMapper.deleteWmsOrderLineById(wmsOrder.getId());
        insertWmsOrderLine(wmsOrder);
        return wmsOrderMapper.updateWmsOrder(wmsOrder);
    }

    /**
     * 批量删除跟物品有关的订单
     *
     * @param ids 需要删除的跟物品有关的订单主键
     * @return 结果
     */
    @Transactional
    public int deleteWmsOrderByIds(Long[] ids) {
        wmsOrderMapper.deleteWmsOrderLineByIds(ids);
        return wmsOrderMapper.deleteWmsOrderByIds(ids);
    }

    /**
     * 删除跟物品有关的订单信息
     *
     * @param id 跟物品有关的订单主键
     * @return 结果
     */
    @Transactional
    public int deleteWmsOrderById(Long id) {
        wmsOrderMapper.deleteWmsOrderLineById(id);
        return wmsOrderMapper.deleteWmsOrderById(id);
    }

    /**
     * 新增物品订单明细信息
     *
     * @param wmsOrder 跟物品有关的订单对象
     */
    public void insertWmsOrderLine(WmsOrder wmsOrder) {
        List<WmsOrderLine> wmsOrderLineList = wmsOrder.getWmsOrderLineList();
        Long id = wmsOrder.getId();
        if (StringUtils.isNotNull(wmsOrderLineList)) {
            List<WmsOrderLine> list = new ArrayList<WmsOrderLine>();
            for (WmsOrderLine wmsOrderLine : wmsOrderLineList) {
                wmsOrderLine.setId(id);
                list.add(wmsOrderLine);
            }
            if (list.size() > 0) {
                wmsOrderMapper.batchWmsOrderLine(list);
            }
        }
    }

    public List<WmsOrder> getOrderLine(Long id) {
        return wmsOrderMapper.getOrderLine(id);
    }


}
